package com.sdkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdkj.mapper.CheckitemMapper;
import com.sdkj.pojo.Checkitem;
import com.sdkj.service.CheckitemService;
import com.sdkj.util.PageResult;
import com.sdkj.util.QueryPageBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

//如何实现计划具体执行过程
@Service
public  class CheckitemServiceImpl implements CheckitemService
{
    @Resource
    private CheckitemMapper checkitemMapper;

    @Override
    public boolean addItemInfo(Checkitem checkitem) {
        if (checkitem.getId()!=null&& checkitem.getId()>0){
            return checkitemMapper.updateById(checkitem)>0;
        }else{
            return checkitemMapper.insert(checkitem)>0;
        }
    }

    @Override
    public PageResult findPageInfo(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        //mybatisplus 提供的条件查询工具
        LambdaQueryWrapper<Checkitem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (queryString!=null && queryString.length()>0){
            //拼接查询条件
            lambdaQueryWrapper.like(Checkitem::getCode,queryString);
            lambdaQueryWrapper.or();
            lambdaQueryWrapper.like(Checkitem::getName,queryString);
        }
        Integer currentPage = queryPageBean.getCurrentPage(); //从前端获取当前页
        Integer pageSize = queryPageBean.getPageSize(); //从前端获取每页展示数据的条数
        //查询数据
        Page<Checkitem> page = new Page<>(currentPage,pageSize);
        Page<Checkitem> newPage = checkitemMapper.selectPage(page, lambdaQueryWrapper);
        long total = newPage.getTotal(); //表中的总记录数
        List<Checkitem> records = newPage.getRecords(); //展示的数据
        //将数据包装成前端需要的结果类型
        PageResult pageResult = new PageResult(total,records);
        return pageResult;
    }

    @Override
    public boolean deleteInfoById(Integer id) {
        return checkitemMapper.deleteById(id)>0;
    }
//删除数据的方法
    @Override
    public Checkitem getItemInfoById(Integer id) {
        return checkitemMapper.selectById(id);
    }
    @Override
    public  List <Checkitem> getAllItemInfo(){
        List <Checkitem> list=checkitemMapper.selectList(null);
        return list;
    }
}
